/**
 * 敲键盘的贼 - 主入口文件
 * 负责初始化游戏和管理游戏生命周期
 */

// 全局游戏实例
let gameInstance;

// 等待DOM加载完成
document.addEventListener('DOMContentLoaded', () => {
    console.log('DOM加载完成');
    
    // 创建游戏实例
    gameInstance = new Game();
    console.log('游戏实例已创建');
    
    // 创建开始界面
    const startScreen = new StartScreen(gameInstance);
    
    // 显示开始界面
    startScreen.show();
    
    console.log('游戏初始化完成');
});

/**
 * 加载游戏所需的资源
 * @returns {Promise} 资源加载的Promise
 */
function loadGameResources() {
    return new Promise((resolve, reject) => {
        // 这里可以加载游戏所需的图片、音频、文本等资源
        // 为简化示例，这里直接返回成功
        setTimeout(resolve, 500);
        
        // 实际项目中应该加载所有资源，例如：
        /*
        const imagesToLoad = [
            'assets/images/sprites/thief/walking.png',
            'assets/images/sprites/thief/running.png',
            // 更多图片...
        ];
        
        const audioToLoad = [
            'assets/audio/sfx/typing.mp3',
            'assets/audio/music/background.mp3',
            // 更多音频...
        ];
        
        const textsToLoad = [
            'assets/texts/easy/text1.json',
            'assets/texts/medium/text1.json',
            // 更多文本...
        ];
        
        // 加载所有资源
        Promise.all([
            loadImages(imagesToLoad),
            loadAudio(audioToLoad),
            loadTexts(textsToLoad)
        ])
        .then(resolve)
        .catch(reject);
        */
    });
}

/**
 * 加载图片资源
 * @param {Array} urls 图片URL数组
 * @returns {Promise} 图片加载的Promise
 */
function loadImages(urls) {
    const promises = urls.map(url => {
        return new Promise((resolve, reject) => {
            const img = new Image();
            img.onload = () => resolve(img);
            img.onerror = () => reject(new Error(`Failed to load image: ${url}`));
            img.src = url;
        });
    });
    
    return Promise.all(promises);
}

/**
 * 加载音频资源
 * @param {Array} urls 音频URL数组
 * @returns {Promise} 音频加载的Promise
 */
function loadAudio(urls) {
    // 使用Howler.js加载音频
    const promises = urls.map(url => {
        return new Promise((resolve, reject) => {
            const sound = new Howl({
                src: [url],
                onload: resolve,
                onloaderror: () => reject(new Error(`Failed to load audio: ${url}`))
            });
        });
    });
    
    return Promise.all(promises);
}

/**
 * 加载文本资源
 * @param {Array} urls 文本URL数组
 * @returns {Promise} 文本加载的Promise
 */
function loadTexts(urls) {
    const promises = urls.map(url => {
        return fetch(url)
            .then(response => {
                if (!response.ok) {
                    throw new Error(`Failed to load text: ${url}`);
                }
                return response.json();
            });
    });
    
    return Promise.all(promises);
}

/**
 * 初始化游戏事件监听
 * @param {Game} game 游戏实例
 */
function initGameEvents(game) {
    // 初始化UI组件
    const startScreen = new StartScreen(game);
    const gameHUD = new GameHUD(game);
    const endScreen = new EndScreen(game);
    
    // 难度选择按钮
    const difficultyButtons = document.querySelectorAll('.difficulty-btn');
    difficultyButtons.forEach(button => {
        button.addEventListener('click', () => {
            const difficulty = button.dataset.difficulty;
            
            // 移除其他按钮的选中状态
            difficultyButtons.forEach(btn => btn.classList.remove('selected'));
            
            // 添加当前按钮的选中状态
            button.classList.add('selected');
            
            // 设置游戏难度
            game.difficulty = difficulty;
        });
    });
    
    // 开始按钮
    const startButton = document.getElementById('start-btn');
    if (startButton) {
        startButton.addEventListener('click', () => {
            // 开始游戏
            game.start(game.difficulty);
        });
    }
    
    // 重新开始按钮
    const restartButton = document.getElementById('restart-btn');
    if (restartButton) {
        restartButton.addEventListener('click', () => {
            // 重新开始游戏
            game.start(game.difficulty);
        });
    }
    
    // 返回主页按钮
    const homeButton = document.getElementById('home-btn');
    if (homeButton) {
        homeButton.addEventListener('click', () => {
            // 重置游戏
            game.reset();
            
            // 显示开始界面
            const uiManager = new UIManager(game);
            uiManager.showScreen('start');
        });
    }
}
